<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/27
 * Time: 5:44 PM
 */

namespace App\BO\OrderStatus\State\Refund;

use App\BO\OrderStatus\Events\OrderApplyThirdPartRefundEvent;
use App\BO\OrderStatus\Events\OrderUserCancelledRefundEvent;
use App\BO\OrderStatus\OrderStateContext;
use App\BO\OrderStatus\OrderStateDefinition;
use App\BO\OrderStatus\State\AOrderState;
use App\BO\OrderStatus\State\DistributingState;
use App\BO\OrderStatus\State\WaitingServiceState;
use App\Entities\OrderRefundEntity;
use Illuminate\Support\Facades\DB;

/**
 * 退款申请后台审核中
 * Class RefundWaitingAdminCheckState
 * @package App\BO\OrderStatus\State\Refund
 */
class RefundWaitingAdminCheckState extends AOrderState
{
    protected $desc = '退款审核中';

    /**
     * @return bool
     */
    public function hasRefundInfo()
    {
        return true;
    }

    /**
     * 通过后台的审核，向第三方平台申请退款
     * @throws \Exception
     */
    public function applyThirdPartRefund()
    {
        // 将状态设置为等待第三方平台通知
        $this->orderStateModifyStore->toRefundWaitingThirdPartNotify();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundWaitingThirdPartNotifyState::class));

        event(new OrderApplyThirdPartRefundEvent($this->stateContext->getOrder()));
    }

    /**
     * 管理员拒绝退款
     */
    public function adminRejectRefund()
    {
        DB::beginTransaction();

        $orderEntity = $this->getOrder();
        $orderRefundEntity = OrderRefundEntity::findByOrderId($orderEntity->getId());

        $orderRefundEntity->setRefundStatus(OrderStateDefinition::STATE_REFUND_REJECT_BY_ADMIN);
        $orderRefundEntity->save();

        $preOrderState = $orderRefundEntity->getPreOrderState();
        if (!$preOrderState) {
            // 老数据
            $this->orderStateModifyStore->toRefundAdminReject();
            $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundAdminRejectState::class));
        } else {
            $orderEntity->setOrderState($preOrderState);
            $orderEntity->save();

            if ($preOrderState == OrderStateDefinition::STATE_WAITING_SERVICE) {
                $this->stateContext->setOrderState(OrderStateContext::getInstance(WaitingServiceState::class));
            } elseif ($preOrderState == OrderStateDefinition::STATE_WAITING_DISTRIBUTION) {
                $this->stateContext->setOrderState(OrderStateContext::getInstance(DistributingState::class));
            }
        }
        DB::commit();
    }

    /**
     * 用户主动取消退款请求
     */
    public function userCancelledRefund()
    {
        $this->orderStateModifyStore->toRefundCancelledByUser();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundCancelledByUserState::class));

        event(new OrderUserCancelledRefundEvent($this->getOrder()));
    }

    /**
     * 模拟的支付不经过第三方平台，直接设置退款状态
     * @param bool $success
     */
    public function refund(bool $success = true)
    {
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundWaitingThirdPartNotifyState::class));
        $this->stateContext->refund();
    }
}
